<template>
  <div class="addAndEdit base-addAndEdit">
    <h3>用户信息</h3>
    <el-form :model="form" label-width="100px" :disabled="type != 'add'">
      <el-row>
        <el-col :span="12">
          <el-form-item label="用户名称">
            <el-input v-model="form.username" placeholder="请输入用户名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="密码">
            <el-input v-model="form.password" placeholder="请输入密码" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="邮箱">
            <el-input v-model="form.email" placeholder="请输入邮箱" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系电话">
            <el-input v-model="form.phoneNum" placeholder="请输入联系电话" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="用户状态">
            <el-select v-model="form.status" placeholder="请选择用户状态">
              <el-option label="开启" value="1" />
              <el-option label="关闭" value="2" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-row justify="center">
      <template v-if="type == 'add'">
        <el-button type="primary" @click="handleSaveOrBack(true)"
          >保存</el-button
        >
      </template>
      <el-button @click="handleSaveOrBack(false)">返回</el-button>
    </el-row>

    <!-- 添加的时候不需要展示角色表格 -->
    <el-table
      v-if="type !== 'add'"
      :data="tableData"
      style="margin: 20px 0"
      row-key="id"
      border
      default-expand-all
      :tree-props="{ children: 'permissions' }"
    >
      <el-table-column prop="roleName" label="名称" />
      <el-table-column prop="roleDesc" label="描述" />
    </el-table>
  </div>
</template>

<script setup>
import { ElMessage } from "element-plus";
import { onMounted, ref } from "vue";
import Mitt from "@/mitt/index.js";

const emits = defineEmits(["sendData"]);
//从viewAll传递到的数据，用来判断当前组件的状态是添加还是编辑
const { currentShow } = defineProps(["currentShow"]);
//defineProps不支持二次解构
const { type, row } = currentShow;

//表单数据
const form = ref({
  username: "",
  password: "",
  email: "",
  phoneNum: "",
  status: "",
});

const handleSaveOrBack = (type) => {
  //如果是保存，调用保存的接口，否则直接返回
  if (type) {
    console.log(form.value);
  }

  emits("sendData", {
    showComponents: "viewAll",
    type: "",
  });
};

const tableData = ref([]);

//表单数据回显
const getList = () => {
  //通过传递的数据id请求接口
  // console.log(row);

  let res = {
    data: {
      id: 1,
      email: "user@qq.com",
      username: "user",
      password: "$2a$10$Sd/7dL0tzHtRaVdntVHiHOwOPkpP1srfYVEafihVjpdpfIu.yYqzS",
      phoneNum: "18513798456",
      status: 1,
      roles: [
        {
          id: 2,
          roleName: "系统用户",
          roleDesc: "ROLE_USER",
          permissions: [
            {
              id: 1,
              permissionName: "查看全部产品",
              url: "product:queryList",
              roles: null,
            },
            {
              id: 8,
              permissionName: "查看全部权限",
              url: "permission:queryList",
              roles: null,
            },
            {
              id: 9,
              permissionName: "查看单个用户",
              url: "user:query",
              roles: null,
            },
            {
              id: 10,
              permissionName: "查看全部角色",
              url: "role:queryList",
              roles: null,
            },
            {
              id: 12,
              permissionName: "查看全部订单",
              url: "order:queryList",
              roles: null,
            },
            {
              id: 13,
              permissionName: "查看单个订单",
              url: "order:query",
              roles: null,
            },
            {
              id: 16,
              permissionName: "查看全部用户",
              url: "user:queryList",
              roles: null,
            },
          ],
          users: null,
        },
      ],
    },
  };
  let { username, password, email, phoneNum, status, roles } = res.data;

  form.value = {
    username,
    password,
    email,
    phoneNum,
    status,
  };

  // 转换 permissions 数组中的字段
  roles.forEach((role) => {
    role.permissions = role.permissions.map((permission) => ({
      roleName: permission.permissionName,
      roleDesc: permission.url,
    }));
  });

  tableData.value = roles;
};

onMounted(() => {
  Mitt.emit("sendcrumbsInfo", { first: "用户管理", second: "用户表单" });
  if (type != "add") {
    //如果状态不处在添加状态时，需要请求接口回显数据
    getList();
  }
});
</script>

<style lang="less" scoped></style>
